package com.bosssoft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibBankDTO;
import com.bosssoft.entity.po.FibAuthPO;
import com.bosssoft.entity.po.FibBankPO;
import com.bosssoft.entity.vo.FibAuthVO;
import com.bosssoft.entity.vo.FibBankVO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibBankService;
import com.bosssoft.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 项昊天
 * @create 2021/8/17 20:50
 */
@Slf4j
@RestController
@RequestMapping("/bank")
public class BankController {

    @Resource
    FibBankService bankService;

    @PostMapping("/save")
    public Result saveBank(@RequestBody @Validated FibBankVO bankVO) {
        try {
            Byte deleted = 1;
            bankVO.setDeleted(deleted);
            bankVO.setBankPassword(MD5Util.getMD5(bankVO.getBankPassword()));
            FibBankDTO bankDTO = new FibBankDTO();
            BeanUtils.copyProperties(bankVO, bankDTO);
            if (bankService.addBank(bankDTO)) {
                return Result.ok().message("新建银行用户成功!");
            }
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
        return Result.error().message("新建银行用户失败!");
    }

    @PutMapping("/modify")
    public Result modifyBank(@RequestBody @Validated FibBankVO bankVO) {
        try {
            bankVO.setBankPassword(MD5Util.getMD5(bankVO.getBankPassword()));
            FibBankDTO bankDTO = new FibBankDTO();
            BeanUtils.copyProperties(bankVO, bankDTO);
            if (bankService.modifyBank(bankDTO)) {
                return Result.ok().message("修改银行用户成功!");
            }
            return Result.error().message("没有修改!");
        } catch (Exception e) {
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PostMapping("/getBankPage")
    public Result getBankPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize, FibBankVO fibBankVO) {
        try {
            Page<FibBankPO> page = new Page<>(pageNo, pageSize);
            QueryWrapper<FibBankPO> queryWrapper = new QueryWrapper<>();
            if (ObjectUtil.isNotNull(fibBankVO.getBankId())) {
                queryWrapper.like("bank_id", fibBankVO.getBankId());
            }
            if (ObjectUtil.isNotNull(fibBankVO.getBankName())) {
                queryWrapper.like("bank_name", fibBankVO.getBankName());
            }
            Page<FibBankPO> bankPage = bankService.page(page, queryWrapper);
            long total = bankPage.getTotal();
            List<FibBankPO> recordPOs = bankPage.getRecords();
            PageResult<FibBankPO> results = new PageResult<>(page, recordPOs);
            return Result.ok().data("total", total)
                    .data("page", results)
                    .data("pageNo", pageNo);
        } catch (Exception e) {
            log.info("getBankPage出现异常");
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @PutMapping("/ban/{id}/{status}")
    public Result banBank(@PathVariable("id") Long id,
                          @PathVariable("status") Integer status) {

        if (bankService.banBankById(id, status)) {
            return Result.ok().message("操作成功!");
        }

        return Result.error().message("操作失败!");
    }

    @DeleteMapping("delete")
    public Result delBank(@RequestBody FibBankVO bankVO) {

        if (bankVO.getBankId() != null && bankService.deleteBankById(bankVO.getBankId())) {
            return Result.ok().message("删除成功!");
        }
        return Result.error().message("删除失败!");
    }

//  @DeleteMapping("delete/{id}")
//  public Result delBank(@PathVariable("id") Long id) {
//
//    if (bankService.deleteBankById(id)) {
//      return Result.ok().message("删除成功!");
//    }
//    return Result.error().message("删除失败!");
//  }

}
